package offer;

import java.util.Stack;

public class  Offer30 {

public  class   MinStack{
    Stack st1,st2;
    public MinStack() {
     st1= new Stack<Integer>();
        st2= new Stack<Integer>();
    }
    public void push(int x) {
        if (st1.isEmpty()||(Integer)st1.peek()>=x){
            st1.push(x);
        }
        st2.push(x);
    }

    public void pop() {
        if (!st2.isEmpty()){
            Integer pop =(Integer) st2.pop();
            if (!st1.isEmpty()&&st1.peek().equals(pop)){
                st1.pop();
            }
        }
    }

    public int top() {
        if (st2.isEmpty()){
            return -1;
        }
        return (Integer) st2.peek();
    }

   public int min() {
        return  (Integer) st1.peek();
    }

}



    class MinStack1 {
        Stack<Integer> stack1 ;
        Stack<Integer> stack2 ;
        /** initialize your data structure here. */
        public MinStack1() {
            stack1 = new Stack();
            stack2 = new Stack();
        }

        public void push(int x) {
            if(stack2.isEmpty() || stack2.peek()>=x)
                stack2.push(x);
            stack1.push(x);

        }

        public void pop() {
            if(!stack2.isEmpty() && stack1.peek()<=stack2.peek())
                stack2.pop();
            stack1.pop();
        }


        public int top() {
            return stack1.isEmpty() ? 0 : stack1.peek();
        }

        public int min() {
            return stack2.peek();
        }
    }

    public static void main(String[] args) {
        Offer30 offer30 = new Offer30();
        MinStack minStack = offer30.new MinStack();

        minStack.push(512);

        minStack.push(-1024);

        minStack.push(-1024);
        minStack.push(512);
        minStack.pop();
        System.out.println(minStack.min());
        minStack.pop();
        System.out.println(minStack.min());
        minStack.pop();
        System.out.println(minStack.min());


    }


//
//    "MinStack","push","push","push","push","pop","min","pop","min","pop","min"]
//            [[],[512],[-1024],[-1024],[512],[],[],[],[],[],[]]

//    [null,null,null,null,null,null,-1024,null,-1024,null,512]

}
